<?php


namespace app\admin\controller;

use app\common\controller\Base;
use app\common\model\Admin;
use app\common\model\AuthGroupAccess;
use app\common\model\AuthRule;
use think\captcha\facade\Captcha;
use think\exception\ValidateException;
use think\facade\Session;

class Login extends Base
{
    /**
     *
     * @return \think\response\View
     */
    public function index()
    {
        if( $this->request->isPost()) {
            //接收参数
            $param = $this->request->param();
            // 验证参数
            $validate = new \app\admin\validate\Login();
            if (!$validate->scene('login')->check($param))   $this->error($validate->getError());
            //验证用户名或密码是否正确
            $data = [
                'username' => $param['username'],
                'password' => xn_encrypt($param['password']),
            ];
            $admin_data =  \app\common\business\Login::login($data)->toArray();

            if( empty($admin_data) ) $this->error('用户名或密码不正确');

            if($admin_data['status']!=1) $this->error('您的账户已被禁用');

            Session::set(config('admin.admin_session'), $admin_data);
            xn_add_admin_log('后台登录');
            $this->success('登录成功', url('admin/index'));
        }
        return view();
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        Session::set(config('admin.admin_session'),null);
        $this->redirect(url('index'));
    }

    /**
     * 生成验证码
     * @return \think\Response
     */
    public function verify()
    {
        return Captcha::create();
    }
}
